package com.qianfeng.openplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openplatform.commons.field.RedisKeyPrefix;
import com.qianfeng.openplatform.webmaster.client.CacheClient;
import com.qianfeng.openplatform.webmaster.entry.LayuiDataTableSource;
import com.qianfeng.openplatform.webmaster.entry.OpenApiMaster;
import com.qianfeng.openplatform.webmaster.mapper.ApiServiceParamMapper;
import com.qianfeng.openplatform.webmaster.pojo.ApiServiceParam;
import com.qianfeng.openplatform.webmaster.service.ApiServiceParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 江江
 * @Date 2020/7/27 12:46
 */
@Service
public class ApiServiceParamServiceImpl implements ApiServiceParamService {
    @Autowired
    private ApiServiceParamMapper apiServiceParamMapper;
    @Autowired
    private CacheClient cacheClient;

    @Override
    public LayuiDataTableSource findApiserviceToPage(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<ApiServiceParam> paramList = apiServiceParamMapper.findApiServiceParamAll();
        PageInfo<ApiServiceParam> info = new PageInfo<>(paramList);
        LayuiDataTableSource source = new LayuiDataTableSource();
        source.setCount(info.getTotal());
        source.setData(info.getList());
        return source;
    }

    @Override
    public OpenApiMaster addApiServiceParam(ApiServiceParam apiServiceParam) {
        int code = apiServiceParamMapper.insertApiServiceParam(apiServiceParam);
        if (code > 0) {
            try {
                cacheClient.sAdd(RedisKeyPrefix.WEB_MASTER_API_SERVICE_PARAM_KEY + apiServiceParam.getServiceName(), apiServiceParam.getName(), -1l);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("添加失败");
    }

    @Override
    public OpenApiMaster updateApiServiceParam(ApiServiceParam apiServiceParam) {
        int code = apiServiceParamMapper.updateApiServiceParam(apiServiceParam);
        if (code > 0) {
            try {
                if (apiServiceParam.getState() == 1) {//redis添加

                    cacheClient.sAdd(RedisKeyPrefix.WEB_MASTER_API_SERVICE_PARAM_KEY + apiServiceParam.getServiceName(), apiServiceParam.getName(), -1l);
                } else {//删除
                    cacheClient.sRemove(RedisKeyPrefix.WEB_MASTER_API_SERVICE_PARAM_KEY + apiServiceParam.getServiceName(), apiServiceParam.getName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("修改失败");
    }

    @Override
    public OpenApiMaster delApiServiceParam(int[] ids) {
        List<ApiServiceParam> apiServiceParamList = apiServiceParamMapper.findListToIds(ids);
        int code = apiServiceParamMapper.delApiServicePatam(ids);
        if (code == ids.length) {
            for (ApiServiceParam param : apiServiceParamList) {
                try {
                    cacheClient.sRemove(RedisKeyPrefix.WEB_MASTER_API_SERVICE_PARAM_KEY + param.getServiceName(), param.getName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return OpenApiMaster.ok();
        }
        return OpenApiMaster.error("删除失败");
    }
}
